//
//  SettingFeedbackController.m
//  QuickCure1
//
//  Created by zhouxuanhe on 16/10/31.
//  Copyright © 2016年 maoge. All rights reserved.
//

#import "SettingFeedbackController.h"
#import "SettingFeedbackCell.h"
#import "SVProgressHUD.h"
#import "ProgressManager.h"

@interface SettingFeedbackController ()<UITextViewDelegate,UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UILabel *lbl_placeHolder;
@property (weak, nonatomic) IBOutlet UITextView *TV_feedBack;

@property (weak, nonatomic) IBOutlet UILabel *lbl_stirngLength;
@property (weak, nonatomic) IBOutlet UIButton *btn_commit;


@property (weak, nonatomic) UITableView *tableView;


@end
static NSString *cellFeedback = @"cellFeedback";
@implementation SettingFeedbackController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:239.0/255 green:239.0/255.0 blue:239.0/255.0 alpha:1];
   
    self.navigationController.title = @"反馈意见";
    //添加方法
    [self setupUI];
    [self setupTableView];
}

#pragma mark -
#pragma mark - 初始化界面
-(void)setupUI
{
    //设置属性
    self.TV_feedBack.delegate = self;
    self.lbl_placeHolder.userInteractionEnabled = NO;
    self.btn_commit.userInteractionEnabled = NO;
    
    //边界宽度,边界颜色
    self.TV_feedBack.layer.borderWidth = 0.2;
    self.TV_feedBack.layer.borderColor = [UIColor lightGrayColor].CGColor;
    //让文字从顶部输入
    self.automaticallyAdjustsScrollViewInsets = NO;
}

#pragma mark -
#pragma mark - 11
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text
{
    //这个判断相当于是textfield中的点击return的代理方法
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];//关闭键盘
        return NO;
    }
    
    //在输入过程中 判断加上输入的字符 是否超过限定字数
    NSString *str = [NSString stringWithFormat:@"%@%@", textView.text, text];
    if (str.length > 30)
    {
        textView.text = [textView.text substringToIndex:30];
        return NO;
    }
    return YES;
}




#pragma mark -
#pragma mark - 改变textView的时会调用
- (void)textViewDidChange:(UITextView *)textView
{
   // NSLog(@"%@", textView.text);
    
    self.lbl_placeHolder.hidden = YES;
    //允许提交按钮点击操作
    
    self.btn_commit.userInteractionEnabled = YES;
    self.btn_commit.backgroundColor = [UIColor colorWithRed:46.0/255 green:197.0/255.0 blue:197.0/255.0 alpha:1];
    //实时显示字数
    self.lbl_stirngLength.text = [NSString stringWithFormat:@"%lu/30", (unsigned long)textView.text.length];
    
    //字数限制操作
    if (textView.text.length >= 30) {
        
        textView.text = [textView.text substringToIndex:30];
        self.lbl_stirngLength.text = @"30/30";
        
    }
    //取消按钮点击权限，并显示提示文字
    if (textView.text.length == 0) {
        
        self.lbl_placeHolder.hidden = NO;
        self.btn_commit.userInteractionEnabled = NO;
        self.btn_commit.backgroundColor = [UIColor lightGrayColor];
        
    }
    
}


#pragma mark -
#pragma mark - 初始化界面
-(void)setupTableView
{
    //创建tableView
    UITableView *tableView = [[UITableView alloc] init];
    _tableView = tableView;
    //代理,数据源
    _tableView.delegate = self;
    _tableView.dataSource = self;
    // 禁止滚动
    self.tableView.scrollEnabled =NO;
    //
    //添加view
    [self.view addSubview:tableView];
    
    //注册
    [tableView registerNib:[UINib nibWithNibName:@"SettingFeedbackCell" bundle:nil] forCellReuseIdentifier:cellFeedback];
//    [tableView registerClass:[SettingFeedbackCell class] forCellReuseIdentifier:cellFeedback];
    //约束
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.leading.mas_equalTo(self.view);
        make.top.mas_equalTo(_TV_feedBack.mas_bottom).mas_offset(40);
        make.height.mas_equalTo(112);
    }];
}

#pragma mark -
#pragma mark - 数据源
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SettingFeedbackCell *cell = [tableView dequeueReusableCellWithIdentifier:cellFeedback forIndexPath:indexPath];
    if (indexPath.row == 0) {
        cell.imgView.image = [UIImage imageNamed:@"dianhua"];
        cell.lbl_phoneNum.text = @"客服电话:18767100325";
    }else{
        cell.imgView.image = [UIImage imageNamed:@"qq"];
        cell.lbl_phoneNum.text = @"用户交流群:4243454214";
    }
    
    
    return cell;
}

#pragma mark -
#pragma mark - 组头
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *headerStr = @"欢迎联系我们";
 
    return headerStr;
}


#pragma mark -
#pragma mark - 点击cell的时候调用
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%zd,%zd",indexPath.section,indexPath.row);
    if (indexPath.row == 0) {
      
        //(UIAlertControllerStyle) 展示的类型
        UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否与18767100325进行通话" preferredStyle:UIAlertControllerStyleAlert];
        
        //默认只有标题 没有操作的按钮:添加操作的按钮 UIAlertAction
        
        UIAlertAction *cancelBtn = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"取消");
        }];
        
        //添加确定
        UIAlertAction *sureBtn = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"确定");
              [self callAction];
        }];
        
        //将action添加到控制器
        [alertVc addAction:cancelBtn];
        [alertVc addAction:sureBtn];
        
        //展示
        [self presentViewController:alertVc animated:YES completion:nil];
    }else if (indexPath.row == 1){
        //(UIAlertControllerStyle) 展示的类型
        UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否加入用户群4243454214" preferredStyle:UIAlertControllerStyleAlert];
        
        //默认只有标题 没有操作的按钮:添加操作的按钮 UIAlertAction
        
        UIAlertAction *cancelBtn = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"取消");
        }];
        
        //添加确定
        UIAlertAction *sureBtn = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"确定");
             [SVProgressHUD showSuccessWithStatus:@"恭喜你成为我们的一员!"];
        }];
        
        //将action添加到控制器
        [alertVc addAction:cancelBtn];
        [alertVc addAction:sureBtn];
        
        //展示
        [self presentViewController:alertVc animated:YES completion:nil];
    }
    
    /****************************************************/
    //根据indexPath获取cell
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    //取消选中状态
    [cell setSelected:NO];
}
#pragma mark -
#pragma mark - 打电话的方法
- (void)callAction{
    
    NSLog(@"打电话!");
    
    NSMutableString *str = [[NSMutableString alloc]initWithFormat:@"tel:%@",@"18767100325"];
    
    UIWebView *callWebView = [[UIWebView alloc]init];
    
    [callWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    
    [self.view addSubview:callWebView];
 
}


#pragma mark -
#pragma mark - 按钮点击,提交反馈意见
- (IBAction)buttonCommitClick:(UIButton *)sender {
    
    if(self.TV_feedBack.text.length > 0){
        self.TV_feedBack.text = nil;
        [self textViewDidChange:_TV_feedBack];
       // [SVProgressHUD showSuccessWithStatus:@"提交成功!"];
        [ProgressManager alertSuccessStatus:@"提交成功!"];
        
    }else{
        [SVProgressHUD showErrorWithStatus:@"反馈不能为空,请重新输入!"];
    }
    [self.view endEditing:YES];
    
    
}



@end
